package kiwi.assist.dev.guieditor.mediator {
	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.event.GUIOperateEvent;
	import kiwi.assist.dev.guieditor.model.EnumConfig;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.model.PropertyConfig;
	import kiwi.assist.dev.guieditor.model.UiConfig;
	import kiwi.assist.dev.guieditor.view.PropertyTable;

	import org.aswing.event.ListItemEvent;
	import org.aswing.event.TableCellEditEvent;
	import org.aswing.ext.GridListItemEvent;
	import org.aswing.table.PropertyTableModel;
	import org.robotlegs.mvcs.Mediator;

	import flash.display.DisplayObject;
	import flash.events.Event;

	/**
	 * 属性列表
	 * @author zhangming.luo
	 */
	public class PropertyTableMediator extends Mediator {
		[Inject]
		public var config : GUIEditorConfig;
		[Inject]
		public var view : PropertyTable;
		/**
		 * 当前的操作
		 */
		private var targetUi : DisplayObject;
		/**
		 * 目标的ui配置
		 */
		private var targetUiConfig : UiConfig;

		override public function onRegister() : void {
			super.onRegister();
			addContextListener(GUIEditorEvent.CONFIG_CHANGE, handleConfigChange);
			addContextListener(GUIOperateEvent.START_EDIT, handlerStartEdit, GUIOperateEvent, false, 0, true);
			addContextListener(GUIOperateEvent.UI_REMOVE, handleRemove, GUIOperateEvent, false, 0, true);
			view.addEventListener(ListItemEvent.ITEM_DOUBLE_CLICK, handleDoubleClick, false, 0, true);
			view.addEventListener(ListItemEvent.ITEM_CLICK, handleClick, false, 0, true);
			view.addEventListener(TableCellEditEvent.EDITING_STOPPED, handleEditingStopped, false, 0, true);
		}

		private function handleChange(event : Event) : void {
			view.updateNow();
		}

		private function handleRemove(event : GUIOperateEvent) : void {
			if (event.targetUi == targetUi) {
				targetUi.removeEventListener(Event.CHANGE, handleChange, false);
			}
			targetUi = null;
			view.reset();
		}

		private function handleEditingStopped(event : TableCellEditEvent) : void {
			var propertyConfig : PropertyConfig = PropertyTableModel(view.getModel()).getList().getElementAt(event.getRow()).value;
			if (propertyConfig.value is EnumConfig) {
				targetUi[propertyConfig.key] = EnumConfig(propertyConfig.value).value;
			} else {
				targetUi[propertyConfig.key] = propertyConfig.value;
			}
			targetUi.dispatchEvent(new Event(Event.CHANGE));
		}

		private function handlerStartEdit(event : GUIOperateEvent) : void {
			if (targetUi) {
				targetUi.removeEventListener(Event.CHANGE, handleChange, false);
			}
			targetUi = event.targetUi;
			if (targetUi == null) {
				view.uiConfig = null;
				view.uiDos = null;
				targetUiConfig = null;
				return;
			}
			targetUiConfig = config.getUiConfigByDos(targetUi);
			view.uiConfig = targetUiConfig;
			view.uiDos = targetUi;
			targetUi.addEventListener(Event.CHANGE, handleChange, false, 0, true);
		}

		private function handleConfigChange(event : GUIEditorEvent) : void {
			// 更新属性
		}

		private function handleClick(event : GridListItemEvent) : void {
		}

		private function handleDoubleClick(event : GridListItemEvent) : void {
		}
	}
}
